package com.china08.yxyapi.api.manage.tec.office.website.article;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SwsTeaArticleDelController extends YxyApiBaseController {

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @Autowired
    SwsArticleRepository swsArticleRepository;

    @ApiOperation("教师资讯详情")
    @RequestMapping(value = { "/api_yw/manage/teaching/article/del",
            "/api_yw/manage/tec/office/website/article/del" }, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        String senderId = getProfile(request, response).getId();
        SwsArticle article = swsArticleRepository.findById(reqModel.id);
        Assert.isTrue(!(article.getAuditStatus().intValue() == 1), "审核通过的不能删除");
        Assert.notNull(article, "资讯不存在");
        Assert.isTrue(StringUtils.equals(senderId, article.getSenderId()), "资讯不存在");
        swsArticleRepository.delete(article);
        return new YxyApiRespModel("删除成功");
    }

    public static class ReqModel {
        public String id;
    }

}
